package com.ljl.org.controller;


import com.ljl.basic.util.AjaxResult;
import com.ljl.basic.util.PageList;
import com.ljl.org.domain.Employee;
import com.ljl.org.query.EmployeeQuery;
import com.ljl.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    // 查询单个
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )

    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    // 查询所有
    @GetMapping()
    @ApiOperation(value = "查询全部" )
    public List<Employee> queryAll(){
        return employeeService.findAll();
    }

    // 删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            employeeService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙请稍后重试！！");
        }
    }

    // 分页和高级查询
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    // 修改或添加
    @PutMapping
    @ApiOperation(value = "添加或修改" )
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() != null) {
                employeeService.update(employee);
            } else {
                employeeService.save(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙请稍后重试！！");
        }
    }
    // 批量删除
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙请稍后重试！！");
        }
    }
    @GetMapping("/list/{shopId}")
    public List<Employee> list(@PathVariable("shopId") Long shopId){
        return employeeService.list(shopId);
    }

}
